package model

import (
	"../global"
	"github.com/go-pg/pg"
)

//未完成订单结构体
type OrderTradeInCompleteModel struct{}

//生成新订单
func (*OrderTradeInCompleteModel) CreateOrder(query *OrderTradeInComplete) (err error) {
	_, err = global.DB.Model(query).Insert()
	return
}

//查找未完成订单
func (*OrderTradeInCompleteModel) GetOrder(query *OrderTradeInComplete, column []string, listParams *ListParams) (result []OrderTradeInComplete, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}
	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.UserID != 0 {
		model.Where("user_id=?", query.UserID)
	}
	if query.ItemID != 0 {
		model.Where("item_id=?", query.ItemID)
	}
	if query.ItemName != "" {
		model.Where("item_name=?", query.ItemName)
	}
	if query.BuyTime != 0 {
		model.Where("buy_time>=?", query.BuyTime)
	}
	if query.Direction == true {
		model.Where("direction=?", true)
	}
	if query.HoldLimit != 0 {
		model.Where("hold_limit=?", query.HoldLimit)
	}
	if query.SellTime != 0 {
		model.Where("sell_time<=?", query.SellTime)
	}

	model.Order("buy_time DESC").Limit(listParams.Limit)
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	}
	err = model.Select()
	return
}

//删除未完成订单
func (*OrderTradeInCompleteModel) DeleteOrder(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&OrderTradeInComplete{}).WhereIn("user_ in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&OrderTradeInComplete{}).Where("user_=?", IDs[0]).Delete()
	}
	return
}

//已完成订单结构体
type OrderTradeCompletedModel struct{}

//获取已完成的订单
func (*OrderTradeCompletedModel) GetOrder(query *OrderTradeCompleted, column []string, listParams *ListParams) (result []OrderTradeCompleted, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}
	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.UserID != 0 {
		model.Where("user_id=?", query.UserID)
	}
	if query.ItemID != 0 {
		model.Where("item_id=?", query.ItemID)
	}
	if query.ItemName != "" {
		model.Where("item_name=?", query.ItemName)
	}
	if query.BuyTime != 0 {
		model.Where("buy_time>=?", query.BuyTime)
	}
	if query.Direction == true {
		model.Where("direction=?", true)
	}
	if query.HoldLimit != 0 {
		model.Where("hold_limit=?", query.HoldLimit)
	}
	if query.SellTime != 0 {
		model.Where("sell_time<=?", query.SellTime)
	}
	if query.IncomeAmount != 0 {
		if query.IncomeAmount > 0 {
			model.Where("income_amount>?", 0)
		}
		if query.IncomeAmount < 0 {
			model.Where("income_amount<?", 0)
		}
	}
	//按卖出时间倒序查询
	model.Order("sell_time DESC").Limit(listParams.Limit)
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	}
	err = model.Select()
	return
}

//删除未完成订单
func (*OrderTradeCompletedModel) DeleteOrder(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&OrderTradeCompleted{}).WhereIn("user_ in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&OrderTradeCompleted{}).Where("user_=?", IDs[0]).Delete()
	}
	return
}

//充值订单
type RechargeOrderModel struct{}

//获取充值订单
func (*RechargeOrderModel) GetOrder(query *RechargeOrder, column []string, listParams *ListParams) (result []RechargeOrder, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}

	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.UserID != 0 {
		model.Where("user_id=?", query.UserID)
	}
	if query.Amount != 0 {
		model.Where("amount=?", query.Amount)
	}
	if query.Status == true {
		model.Where("status=?", query.Status)
	}
	if query.StartTime != 0 {
		model.Where("start_time>=?", query.StartTime)
	}

	if query.EndTime != 0 {
		model.Where("end_time<=?", query.EndTime)
	}

	if query.Status == true {
		model.Where("status=?", true)
	}
	model.Order("start_time DESC").Limit(listParams.Limit)
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	}
	err = model.Select()
	return
}

//生成充值订单
func (*RechargeOrderModel) CreateOrder(query *RechargeOrder) (err error) {
	_, err = global.DB.Model(query).Insert()
	return
}

//删除充值订单
func (*RechargeOrderModel) DeleteOrder(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&RechargeOrder{}).WhereIn("user_ in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&RechargeOrder{}).Where("user_=?", IDs[0]).Delete()
	}
	return
}

//提现订单
type WithdrawOrderModel struct{}

//生成提现订单
func (*WithdrawOrderModel) CreateOrder(query *WithdrawOrder) (err error) {
	_, err = global.DB.Model(query).Insert()
	return
}

//获取提现订单
func (*WithdrawOrderModel) GetOrder(query *WithdrawOrder, column []string, listParams *ListParams) (result []WithdrawOrder, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}
	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.UserID != 0 {
		model.Where("user_id=?", query.UserID)
	}
	if query.Amount != 0 {
		model.Where("amount=?", query.Amount)
	}
	if query.Status == true {
		model.Where("status=?", true)
	}
	if query.Name != "" {
		model.Where("name=?", query.Name)
	}
	if query.Mobile != "" {
		model.Where("mobile=?", query.Mobile)
	}
	if query.BankNumber != "" {
		model.Where("bank_number=?", query.BankNumber)
	}
	if query.IDCardNumber != "" {
		model.Where("iccard_number=?", query.IDCardNumber)
	}
	if query.StartTime != 0 {
		model.Where("start_time>=?", query.StartTime)
	}
	if query.EndTime != 0 {
		model.Where("end_time<=?", query.EndTime)
	}
	if query.Status == true {
		model.Where("status=?", true)
	}
	//按发起时间倒序查询
	model.Order("start_time DESC").Limit(listParams.Limit)
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	}
	model.Select()
	return
}

//修改提现订单
func (*WithdrawOrderModel) EditOrder(query *WithdrawOrder) (err error) {
	model := global.DB.Model(&WithdrawOrder{})
	model.Where("id=?", query.ID)

	if query.Amount != 0 {
		model.Set("amount=?", query.Amount)
	}
	if query.BankName != "" {
		model.Set("bank_name=?", query.BankName)
	}
	if query.BankBranch != "" {
		model.Set("bank_branch=?", query.BankBranch)
	}
	if query.BankNumber != "" {
		model.Set("bank_number=?", query.BankNumber)
	}
	if query.IDCardNumber != "" {
		model.Set("idcard_number=?", query.IDCardNumber)
	}
	if query.Mobile != "" {
		model.Set("mobile=?", query.Mobile)
	}
	if query.Name != "" {
		model.Set("name=?", query.Name)
	}
	if query.StartTime != 0 {
		model.Set("start_time=?", query.StartTime)
	}
	if query.EndTime != 0 {
		model.Set("end_time=?", query.EndTime)
	}
	if query.Status == true {
		model.Set("status=?", true)
	}
	_, err = model.Update()
	return
}

//删除提现订单
func (*WithdrawOrderModel) DeleteOrder(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&WithdrawOrder{}).WhereIn("user_ in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&WithdrawOrder{}).Where("user_=?", IDs[0]).Delete()
	}
	return
}
